using System.Collections;
using System.Collections.Generic;
using System.Data;
using UnityEngine;

public class NpcAdditional:MonoBehaviour {

	[SerializeField] Material projectileMaterial;
	[SerializeField] float statGrowth = 1;
	[SerializeField] float projectileSpeedMultiplier = 1;
	[SerializeField] float projectileDamageMultiplier = 1;

	static NpcAdditional() {
		Projectile.ProjectileFiring+=Projectile_ProjectileFiring;
		Projectile.ProjectileDestroy+=Projectile_ProjectileDestroy;
	}

	static Dictionary<Projectile,(Material, Material)> materialModifiedProjectiles = new Dictionary<Projectile,(Material, Material)>();
	static Dictionary<Mob,NpcAdditional> instances = new Dictionary<Mob,NpcAdditional>();

	private static void Projectile_ProjectileFiring(object _sender,ProjectileEventArgs e) {
		Mob mob = e.shooterMob;
		if(!mob) return;
		NpcAdditional self = mob.GetComponent<NpcAdditional>();
		if(!self) return;
		Projectile sender = _sender as Projectile;
		e.stat.damage.AddMultiply(self.projectileDamageMultiplier*self.growthAmount,float.MaxValue);
		e.stat.speed.AddMultiply(self.projectileSpeedMultiplier,float.MaxValue);
		if(self.projectileSpeedMultiplier!=0)
			e.stat.timeTotal.AddMultiply(1/self.projectileSpeedMultiplier,float.MaxValue);

		if(!self.projectileMaterial) return;
		SpriteRenderer spriteRenderer = sender.GetComponent<SpriteRenderer>();
		if(!spriteRenderer) return;

		materialModifiedProjectiles.Add(sender,(spriteRenderer.material, self.projectileMaterial));
		spriteRenderer.material=self.projectileMaterial;

	}
	private static void Projectile_ProjectileDestroy(object _sender,ProjectileEventArgs e) {
		Projectile sender = _sender as Projectile;
		if(!materialModifiedProjectiles.ContainsKey(sender)) return;
		var data = materialModifiedProjectiles[sender];
		materialModifiedProjectiles.Remove(sender);
		SpriteRenderer sprite = sender.GetComponent<SpriteRenderer>();
		if(data.Item2.shader==sprite.material.shader) sprite.material=data.Item1;
	}

	Mob mob;
	bool inited;
	public float growthAmount{ get; private set; }
	private void Start() {
		mob=GetComponent<Mob>();
		instances.Add(mob,this);
	}
	private void OnDestroy() {
		if(mob==null) return;
		instances.Remove(mob);
	}

	private void FixedUpdate() {
		if(!inited) {
			inited=true;
			growthAmount = Game.currentLevel.statGrowth;
			//growthAmount=((1+(statGrowth-1)*Game.currentLevel)+Mathf.Pow(statGrowth,Game.currentLevel))/2f;
			mob.IncreaseMaxHP(mob._maxHP*(growthAmount-1),this);
		}
		mob.energy+=1;
	}


}
